import axios from 'axios'
import { getItem, removeItem } from 'utils/stroage'
import { message } from 'antd'
import history from 'utils/history'
// 初始化url
const request = axios.create({
  baseURL: 'http://geek.itheima.net/',
})
// 请求拦截器
request.interceptors.request.use(
  (config) => {
    const token = getItem()
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
// 响应拦截器
request.interceptors.response.use(
  (config) => {
    return config
  },
  (error) => {
    if (error.response.status === 401) {
      removeItem()
      // window.location.href = '/login'
      message.error(error.response.message)
      history.push('/login')
    }
    return Promise.reject(error)
  }
)
export default request
